package com.utils
{
	/**
	 *位图工具类，存储图片数据 
	 *  @author Lufu
	 */	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObjectContainer;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.net.URLRequest;
	
	public class BitmapUtil extends Sprite
	{
		private static var bitmap:Bitmap;

		private static var _loadImageOver:Function = new Function();
		
		/**
		 * 对目标位图数据设置糖纸效果 随机添加count个像素点 颜色随机
		 * @param bmd
		 * @param count
		 */		
		public static function colorPaper(imageCls:Class,count:int):void{
			var bmd:BitmapData = (new imageCls() as Bitmap).bitmapData;
			for (var i:int = 0; i < count; i++) 
			{
				bmd.setPixel(Math.random() * bmd.width,Math.random() * bmd.height,Math.random() * 0xFFFFFF);
			}
		}
		
		/**
		 * 对目标位图数据处理马赛克效果
		 * @param bmd
		 * @param size 马赛克颗粒大小
		 */		
		public static function mosaics(imageCls:Class,size:int = 5):void{
			var bmd:BitmapData = (new imageCls() as Bitmap).bitmapData;
			for (var i:int = 0; i < bmd.width; i += size) 
			{
				for (var j:int = 0; j < bmd.height; j += size) 
				{
					bmd.fillRect(new Rectangle(i,j,size,size),bmd.getPixel(i,j));
				}
			}
		}
		
		/**
		 * 将位图加载到Sprite里面,并返回Sprite对象
		 * @param path 图片路径
		 * @param width 宽度
		 * @param height 高度
		 * @return Sprite
		 */		
		public static function imageLoader(path:String = 'path',width:int = 0,height:int = 0):Sprite{
			var contain:Sprite = new Sprite;
			var loader:Loader = new Loader();
			loader.load(new URLRequest(path));
			loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,function ():void{
				throw Error('请检查图片路径是否正确!')
			});
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE,
				function (e:Event):void{
					bitmap = loader.content as Bitmap;
					contain.addChild(bitmap);
					this.width = bitmap.width;
					this.height = bitmap.height;
					_loadImageOver();
					if(width == 0 || height == 0) return;
					bitmap.width = width;
					bitmap.height = height;
				}
			);
			return contain;
		}
		
		public static function set loadImageOver(value:Function):void{_loadImageOver = value;}
		
		/**
		 * 返回目标二维数组
		 * @param imageCls
		 * @param rows 行数
		 * @param cols 列数
		 */		
		public static function getTargetMatrix(imageCls:Class,rows:int = 1,cols:int = 1):Array
		{
			var bd:BitmapData = (new imageCls() as Bitmap).bitmapData;
			var bdList:Array = [];
			var width:Number = bd.width / cols;
			var height:Number = bd.height / rows;
			for (var i:int = 0; i < rows; i++) 
			{
				var list:Array = [];
				bdList.push(list);
				for (var j:int = 0; j < cols; j++) 
				{
					var copyData:BitmapData = new BitmapData(width,height,true,0);
					copyData.copyPixels(bd,new Rectangle(width*j,height*i,width,height),new Point());
					list.push(copyData);
				}
			}
			return bdList;
		}
		
		/**
		 * 返回目标一维数组
		 * @param imageCls
		 * @param rows 行数
		 * @param cols 列数
		 */
		public static function getTargetList(imageCls:Class,rows:int = 1,cols:int = 1):Array{
			var bd:BitmapData = (new imageCls() as Bitmap).bitmapData;
			var bdList:Array = [];
			var width:Number = bd.width / cols;
			var height:Number = bd.height / rows;
			for (var i:int = 0; i < rows; i++) 
			{
				for (var j:int = 0; j < cols; j++) 
				{
					var copyData:BitmapData = new BitmapData(width,height,true,0);
					copyData.copyPixels(bd,new Rectangle(width*j,height*i,width,height),new Point());
					bdList.push(copyData);
				}
			}
			return bdList;
		}
		
		
		
		
		
		
		
		
		
		
		
		
		
	}
}